package practice;

import java.util.Stack;

public class Day12 {
    public int getLongestPalindrome (String A) {
        int len = A.length();
        int max = 1;
        for(int i = 0; i < len; i++) {
            int beg = i - 1;
            int end = i + 1;
            while(beg >= 0 && end < len) {
                if(A.charAt(beg) == A.charAt(end)) {
                    max = Math.max(max,end-beg+1);
                }else {
                    break;
                }
                end++;
                beg--;
            }
            beg = i;
            end = i + 1;
            while(beg >= 0 && end < len) {
                if(A.charAt(beg) == A.charAt(end)) {
                    max = Math.max(max,end-beg+1);
                }else {
                    break;
                }
                beg--;
                end++;
            }
        }
        return max;
    }

    private int[] dpx = {0,0,1,-1};
    private int[] dpy = {1,-1,0,0};
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        int n = image.length;
        int m = image[0].length;
        int prev = image[sr][sc];
        if(prev == color) {
            return image;
        }
        Stack<Integer[]> stack = new Stack<>();
        image[sr][sc] = color;
        stack.push(new Integer[]{sr,sc});
        while(!stack.isEmpty()) {
            Integer[] ret = stack.pop();
            for(int k = 0; k < 4; k++) {
                int x = ret[0] + dpx[k];
                int y = ret[1] + dpy[k];
                if(x >= 0 && x < n && y >= 0 && y < m && image[x][y] == prev) {
                    stack.push(new Integer[]{x,y});
                    image[x][y] = color;
                }
            }
        }
        return image;
    }
}
